package com.woniuxy.service.impl;

import com.woniuxy.dao.LoginDao;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.LoginService;
import com.woniuxy.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @ClassName LoginServiceImpl
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/5 19:51
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginDao loginDao;
    @Override
    public ResponseEntity login(String username, String password) {
        User user = loginDao.findOneUser(username,password);
        if(Objects.nonNull(user)){
            ResponseEntity responseEntity = new ResponseEntity(user);
            String token= JWTUtils.sign(user.getAccount(),user.getPassword());
            responseEntity.put("token",token);
            return responseEntity;
        }else {
            return ResponseEntity.AUTHFAILED;
        }
    }
}
